/**
 * @Author: SevDaisy十七散人 
 * @Date: 2020-07-08 20:49:17
 */
package zyy.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import zyy.model.BeanAddress;
import zyy.util.BusinessException;
import zyy.util.DBUtil;
import zyy.util.DbException;

public class AddrManager {
  public void add(String Type, int KeyID, String P, String C, String B, String D, String N, String T)
      throws BusinessException {
    Connection conn = null;
    try {
      conn = DBUtil.getConnection();
      String sql;
      switch (Type) {
        case "用户":
          sql = " INSERT INTO" + " Address(UserID,addrProvince,addrCity,addrBlock,addrDetail,targetName,Telphone)"
              + " values(?,?,?,?,?,?,?)";
          break;
        case "商家":
          sql = " INSERT INTO" + " Address(MID,addrProvince,addrCity,addrBlock,addrDetail,targetName,Telphone)"
              + " values(?,?,?,?,?,?,?)";
          break;
        default:
          throw new BusinessException("未指定地址默认归属方类型\n [ 用户 | 商家 ]::" + (Type == null ? "" : Type));
      }
      java.sql.PreparedStatement pst = conn.prepareStatement(sql);
      pst.setInt(1, KeyID);
      pst.setString(2, P);
      pst.setString(3, C);
      pst.setString(4, B);
      pst.setString(5, D);
      pst.setString(6, N);
      pst.setString(7, T);
      pst.executeUpdate();
      pst.close();
    } catch (SQLException e) {
      e.printStackTrace();
      throw new BusinessException(e.getMessage());
    } finally {
      if (conn != null)
        try {
          conn.close();
        } catch (SQLException e) {
          e.printStackTrace();
        }
    }

  }

  public void unadd(int AddressID) {

  }

  public List<BeanAddress> loadByUserID(int UserID) throws DbException {
    List<BeanAddress> result = new ArrayList<BeanAddress>();
    Connection conn = null;
    try {
      conn = DBUtil.getConnection();
      System.out.println("地址读取 by UserID is Called ");
      String sql = "select addrProvince,addrCity,addrBlock,addrDetail,targetName,Telphone,AddressID from Address "
          + " where UserID=?";
      java.sql.PreparedStatement pst = conn.prepareStatement(sql);
      pst.setInt(1, UserID);
      java.sql.ResultSet rs = pst.executeQuery();
      while (rs.next()) {
        BeanAddress p = new BeanAddress();
        p.setUserID(UserID);
        p.setAddrProvince(rs.getString(1));
        p.setAddrCity(rs.getString(2));
        p.setAddrBlock(rs.getString(3));
        p.setAddrDetail(rs.getString(4));
        p.setTargetName(rs.getString(5));
        p.setTelphone(rs.getString(6));
        p.setAddressID(rs.getInt(7));
        result.add(p);
      }
      rs.close();
      pst.close();
    } catch (SQLException e) {
      e.printStackTrace();
      throw new DbException(e);
    } finally {
      if (conn != null)
        try {
          conn.close();
        } catch (SQLException e) {
          e.printStackTrace();
        }
    }
    return result;
  }

  public List<BeanAddress> loadAll() throws DbException {
    List<BeanAddress> result = new ArrayList<BeanAddress>();
    Connection conn = null;
    try {
      conn = DBUtil.getConnection();
      String sql = "select addrProvince,addrCity,addrBlock,addrDetail,targetName,Telphone,AddressID,UserID,MID from Address";
      java.sql.PreparedStatement pst = conn.prepareStatement(sql);
      java.sql.ResultSet rs = pst.executeQuery();
      while (rs.next()) {
        BeanAddress p = new BeanAddress();
        p.setAddrProvince(rs.getString(1));
        p.setAddrCity(rs.getString(2));
        p.setAddrBlock(rs.getString(3));
        p.setAddrDetail(rs.getString(4));
        p.setTargetName(rs.getString(5));
        p.setTelphone(rs.getString(6));
        p.setAddressID(rs.getInt(7));
        p.setUserID(rs.getInt(8));
        p.setMID(rs.getInt(9));
        result.add(p);
      }
      rs.close();
      pst.close();
    } catch (SQLException e) {
      e.printStackTrace();
      throw new DbException(e);
    } finally {
      if (conn != null)
        try {
          conn.close();
        } catch (SQLException e) {
          e.printStackTrace();
        }
    }
    return result;
  }

  public static void main(String[] args) {
    AddrManager m = new AddrManager();
    try {
      List<BeanAddress> l = m.loadByUserID(1);
      for (BeanAddress k : l) {
        System.out.println(k);
      }
    } catch (DbException e) {
      e.printStackTrace();
      throw new RuntimeException("数据库 加载地址 失败");
    }
  }
}